<?php


class important extends connector {

	function important() {
		$this->softbase(true);
	}

	function onmodellist() {
		$this->powercheck(62, 63);
		parent::start_template();

		$MinPageid = $this->fun->accept('MinPageid', 'R');

		$page_id = $this->fun->accept('page_id', 'R');

		$countnum = $this->fun->accept('countnum', 'R');

		$MaxPerPage = $this->fun->accept('MaxPerPage', 'R');
		if (empty($MaxPerPage)) {
			$MaxPerPage = $this->CON['max_list'];
		}
		$db_where = ' WHERE mid>0';
		$isclass = $this->fun->accept('isclass', 'R');
		if (!empty($isclass)) {
			if ($isclass == 2) $isclass = 0;
			$db_where.=' AND isclass=' . $isclass;
		}
		$lockin = $this->fun->accept('lockin', 'R');
		if (!empty($lockin)) {
			if ($lockin == 2) $lockin = 0;
			$db_where.=' AND lockin=' . $lockin;
		}
		$isorder = $this->fun->accept('isorder', 'R');
		if (!empty($isorder)) {
			if ($isorder == 2) $isorder = 0;
			$db_where.=' AND isorder=' . $isorder;
		}
		$istime = $this->fun->accept('istime', 'R');
		if (!empty($istime)) {
			if ($istime == 2) $istime = 0;
			$db_where.=' AND istime=' . $istime;
		}
		$ishtml = $this->fun->accept('ishtml', 'R');
		if (!empty($ishtml)) {
			if ($ishtml == 2) $ishtml = 0;
			$db_where.=' AND ishtml=' . $ishtml;
		}
		$iscontent = $this->fun->accept('iscontent', 'R');
		if (!empty($iscontent)) {
			if ($iscontent == 2) $iscontent = 0;
			$db_where.=' AND iscontent=' . $iscontent;
		}


		$limitkey = $this->fun->accept('limitkey', 'R');
		$limitkey = empty($limitkey) ? 'mid' : $limitkey;

		$limitclass = $this->fun->accept('limitclass', 'R');
		$limitclass = empty($limitclass) ? 'DESC' : $limitclass;
		$db_table = db_prefix . 'model';
		if (!empty($countnum)) {
			$countnum = $this->db_numrows($db_table, $db_where);
			exit($countnum);
		}
		$sql = 'SELECT mid,lng,modelname,pagemax,pagesylte,isclass,lockin,isorder,isauthor,istsn,istime,ishtml,iscontent,addtime FROM ' . $db_table . $db_where . ' ORDER BY ' . $limitkey . ' ' . $limitclass . ' LIMIT ' . $MinPageid . ',' . $MaxPerPage;
		$rs = $this->db->query($sql);
		while ($rsList = $this->db->fetch_assoc($rs)) {
			$array[] = $rsList;
		}
		$this->fun->setcookie($this->fun->noncefile() . 'pgid', $page_id, 600);
		$this->ectemplates->assign('array', $array);
		$this->ectemplates->assign('sql', $sql);
		$this->ectemplates->display('article/model_list');
	}

	function onlist() {
		include_once admin_ROOT . adminfile . '/include/command_list.php';
		parent::start_template();
		$listfunction = $this->fun->accept('listfunction', 'G');
		if (empty($listfunction)) {
			exit('err');
		}
		$mid = $this->fun->accept('mid', 'R');

		$cookpage = $this->fun->accept($CONLIST[$listfunction]['pagecoock'], 'C');
		$nowpage = empty($cookpage) ? 0 : $cookpage;
		$this->ectemplates->assign('nowpage', $nowpage);
		$this->ectemplates->assign('loadurl', $CONLIST[$listfunction]['loadurl'] . '&mid=' . $mid);

		$this->ectemplates->assign('MaxPerPage', $this->CON['max_list']);

		$this->ectemplates->assign('MaxHit', $this->CON['max_page']);
		$this->ectemplates->assign('listtype', $listtype);
		$this->ectemplates->assign('listfunction', $listfunction);
		$this->ectemplates->display('article/model_tab_list');
	}

	function onmodelattlist() {
		parent::start_template();

		$MinPageid = $this->fun->accept('MinPageid', 'R');

		$page_id = $this->fun->accept('page_id', 'R');

		$countnum = $this->fun->accept('countnum', 'R');

		$MaxPerPage = $this->fun->accept('MaxPerPage', 'R');
		if (empty($MaxPerPage)) {
			$MaxPerPage = $this->CON['max_list'];
		}
		$wheretext = null;
		$isclass = $this->fun->accept('isclass', 'R');
		if (!empty($isclass)) {
			if ($isclass == 2) $isclass = 0;
			$wheretext.=' AND isclass=' . $isclass;
		}
		$mid = $this->fun->accept('mid', 'R');


		$limitkey = $this->fun->accept('limitkey', 'R');
		$limitkey = empty($limitkey) ? 'aid' : $limitkey;

		$limitclass = $this->fun->accept('limitclass', 'R');
		$limitclass = empty($limitclass) ? 'DESC' : $limitclass;
		$db_where = " WHERE mid=$mid" . $wheretext;
		$db_table = db_prefix . 'model_att';
		if (!empty($countnum)) {
			$countnum = $this->db_numrows($db_table, $db_where);
			exit($countnum);
		}
		$sql = 'SELECT aid,pid,mid,typename,typeremark,attrname,inputtype,attrvalue,attrsize,attrrow,attrlenther,isclass FROM ' . $db_table . $db_where . '
			ORDER BY ' . $limitkey . ' ' . $limitclass . ' LIMIT ' . $MinPageid . ',' . $MaxPerPage;
		$rs = $this->db->query($sql);
		while ($rsList = $this->db->fetch_assoc($rs)) {
			$array[] = $rsList;
		}
		$this->fun->setcookie($this->fun->noncefile() . 'pgid', $page_id, 600);
		$this->ectemplates->assign('array', $array);
		$this->ectemplates->assign('sql', $sql);
		$this->ectemplates->display('article/model_att_list');
	}

	function onmodelpreview() {
		parent::start_template();
		$db_table = db_prefix . 'model';
		$id = $this->fun->accept('id', 'G');
		$db_where = 'mid=' . $id;
		$modelread = $this->db->fetch_first('SELECT * FROM ' . $db_table . ' WHERE ' . $db_where);

		$db_where = ' WHERE isclass=1 and mid=' . $modelread['mid'] . ' ORDER BY pid';
		$db_table = db_prefix . 'model_att';
		$sql = 'SELECT * FROM ' . $db_table . $db_where;
		$rs = $this->db->query($sql);
		while ($rsList = $this->db->fetch_assoc($rs)) {
			if ($rsList['inputtype'] == 'select' || $rsList['inputtype'] == 'radio' || $rsList['inputtype'] == 'checkbox') {
				$forvalue = split("\n", $rsList['attrvalue']);
				$newvalue = array();
				foreach ($forvalue as $key => $forvalue) {
					if ($key == 0) {
						$newvalue[] = array('name' => $forvalue, 'selected' => 'selected');
					} else {
						$newvalue[] = array('name' => $forvalue, 'selected' => '');
					}
				}
				$rsList['attrvalue'] = $newvalue;
			}
			$attrread[] = $rsList;
		}
		$this->ectemplates->assign('modelread', $modelread);
		$this->ectemplates->assign('attrread', $attrread);
		$this->ectemplates->display('article/model_preview');
	}

	function onmodeladd() {
		parent::start_template();
		$tab = $this->fun->accept('tab', 'G');
		$tab = empty($tab) ? 'true' : $tab;

		$this->ectemplates->assign('lng', $lng['list']);
		$this->ectemplates->assign('tab', $tab);
		$this->ectemplates->display('article/model_add');
	}

	function onmodeledit() {
		parent::start_template();
		$db_table = db_prefix . 'model';
		$type = $this->fun->accept('type', 'G');
		$id = $this->fun->accept('id', 'G');
		$db_where = 'mid=' . $id;
		$modelread = $this->db->fetch_first('SELECT * FROM ' . $db_table . ' WHERE ' . $db_where);
		$this->ectemplates->assign('tab', 'true');
		$this->ectemplates->assign('modelread', $modelread);
		if ($type == 'copy') {
			$this->ectemplates->display('article/model_copy');
		} else {
			$this->ectemplates->assign('type', $type);
			$this->ectemplates->display('article/model_edit');
		}
	}

	function onmodelattradd() {
		include_once admin_ROOT . adminfile . '/include/inc_formtypelist.php';
		parent::start_template();
		$id = $this->fun->accept('id', 'G');
		$this->ectemplates->assign('mid', $id);
		$this->ectemplates->assign('tab', 'true');
		$this->ectemplates->assign('formtypelist', $FORMTYPE);
		$this->ectemplates->display('article/model_attr_add');
	}

	function onmodelattredit() {
		include_once admin_ROOT . adminfile . '/include/inc_formtypelist.php';
		parent::start_template();
		$db_table = db_prefix . 'model_att';
		$aid = $this->fun->accept('aid', 'G');
		$db_where = 'aid=' . $aid;
		$modelattrread = $this->db->fetch_first('SELECT * FROM ' . $db_table . ' WHERE ' . $db_where);
		$this->ectemplates->assign('tab', 'true');
		$this->ectemplates->assign('formtypelist', $FORMTYPE);
		$this->ectemplates->assign('modelattrread', $modelattrread);
		$this->ectemplates->display('article/model_attr_edit');
	}

	function onmodelsave() {
		parent::start_template();
		$inputclass = $this->fun->accept('inputclass', 'P');
		$lng = $this->fun->accept('lng', 'P');
		$lng = empty($lng) ? $this->CON['is_lancode'] : $lng;
		$modelname = $this->fun->accept('modelname', 'P');
		$pagemax = $this->fun->accept('pagemax', 'P');
		$pagesylte = $this->fun->accept('pagesylte', 'P');
		$isorder = $this->fun->accept('isorder', 'P');
		$isexhibit = $this->fun->accept('isexhibit', 'P');
		$istime = $this->fun->accept('istime', 'P');
		$ishtml = $this->fun->accept('ishtml', 'P');
		$islongtitle = $this->fun->accept('islongtitle', 'P');
		$istsn = $this->fun->accept('istsn', 'P');
		$isauthor = $this->fun->accept('isauthor', 'P');
		$iscontent = $this->fun->accept('iscontent', 'P');
		$db_table = db_prefix . 'model';
		$date = time();
		$isclass = $this->esp_inputclassid;
		$isclass = empty($isclass) ? 0 : $isclass;
		if ($inputclass == 'add') {
			$this->powercheck(62, 64);
			$db_field = 'lng,modelname,pagemax,pagesylte,isclass,lockin,isorder,isexhibit,istime,islongtitle,istsn,isauthor,ishtml,iscontent,addtime';
			$db_values = "'$lng','$modelname',$pagemax,$pagesylte,$isclass,0,$isorder,$isexhibit,$istime,$islongtitle,$istsn,$isauthor,$ishtml,$iscontent,$date";
			$this->db->query('INSERT INTO ' . $db_table . ' (' . $db_field . ') VALUES (' . $db_values . ')');
			$this->writelog($this->lng['modelmanage_add_log'], $this->lng['log_extra_ok'] . ' modelname=' . $modelname);
			$this->dbcache->clearcache('model_array_' . $lng, true);
			exit('true');
		} elseif ($inputclass == 'edit') {
			$this->powercheck(62, 93);
			$id = $this->fun->accept('id', 'P');
			$db_where = 'mid=' . $id;
			$db_set = "modelname='$modelname',pagemax=$pagemax,pagesylte=$pagesylte,isorder='$isorder',isexhibit=$isexhibit,istime='$istime',islongtitle=$islongtitle,istsn=$istsn,isauthor=$isauthor,ishtml=$ishtml,iscontent=$iscontent";
			$this->db->query('UPDATE ' . $db_table . ' SET ' . $db_set . ' WHERE ' . $db_where);
			$this->writelog($this->lng['modelmanage_edit_log'], $this->lng['log_extra_ok'] . ' modelname=' . $modelname . ' id=' . $id);
			$this->dbcache->clearcache('model_view_' . $id, true);
			$this->dbcache->clearcache('model_array_' . $lng, true);
			exit('true');
		} elseif ($inputclass == 'copy') {
			$this->powercheck(62, 92);
			$id = $this->fun->accept('id', 'P');
			if (empty($id)) exit('false');

			$db_field = 'lng,modelname,pagemax,pagesylte,isclass,lockin,isorder,isexhibit,istime,islongtitle,istsn,isauthor,ishtml,iscontent,addtime';
			$db_values = "'$lng','$modelname',$pagemax,$pagesylte,$isclass,0,$isorder,$isexhibit,$istime,$islongtitle,$istsn,$isauthor,$ishtml,$iscontent,$date";
			$this->db->query('INSERT INTO ' . $db_table . ' (' . $db_field . ') VALUES (' . $db_values . ')');
			$installid = $this->db->insert_id();
			if (empty($installid)) exit('false');

			$db_where = " WHERE mid=$id";
			$db_table = db_prefix . 'model_att';
			$sql = 'SELECT * FROM ' . $db_table . $db_where;
			$rs = $this->db->query($sql);
			$installstr = "INSERT INTO $db_table (pid,mid,typename,typeremark,attrname,inputtype,attrvalue,attrsize,attrrow,attrlenther,isclass) VALUES";
			$i = 0;
			while ($rsList = $this->db->fetch_assoc($rs)) {
				$i++;
				$installstr.="($rsList[pid],$installid,'$rsList[typename]','$rsList[typeremark]','$rsList[attrname]','$rsList[inputtype]',
				'$rsList[attrvalue]',$rsList[attrsize],$rsList[attrrow],$rsList[attrlenther],$rsList[isclass]),";
			}
			if ($i > 0) {

				$newinstall = substr($installstr, 0, strlen($installstr) - 1);
				$this->db->query($newinstall);
			}
			$this->writelog($this->lng['modelmanage_copy_log'], $this->lng['log_extra_ok'] . ' modelname=' . $modelname . ' id=' . $id);
			$this->dbcache->clearcache('model_array_' . $lng, true);
			exit('true');
		}
	}

	function onmodelattrsave() {
		parent::start_template();
		$inputclass = $this->fun->accept('inputclass', 'P');
		$mid = $this->fun->accept('mid', 'P');
		$typename = $this->fun->accept('typename', 'P');
		$attrname = $this->fun->accept('attrname', 'P');
		if (!empty($attrname)) {
			$attrname = str_replace(' ', '', $attrname);
		} else {
			exit('false');
		}
		$typeremark = $this->fun->accept('typeremark', 'P');
		$inputtype = $this->fun->accept('inputtype', 'P');
		$attrvalue = $this->fun->accept('attrvalue', 'P');

		$attrsize = $this->fun->accept('attrsize', 'P');
		$attrsize = empty($attrsize) ? 20 : $attrsize;

		$attrrow = $this->fun->accept('attrrow', 'P');
		$attrrow = empty($attrrow) ? 5 : $attrrow;

		$attrlenther = $this->fun->accept('attrlenther', 'P');
		$attrlenther = empty($attrlenther) ? 50 : $attrlenther;

		$db_table = db_prefix . 'model_att';
		if ($inputclass == 'add') {
			$this->powercheck(62, 87);
			$db_field = 'pid,mid,typename,typeremark,attrname,inputtype,attrvalue,attrsize,attrrow,attrlenther,isclass';
			$db_values = "50,$mid,'$typename','$typeremark','$attrname','$inputtype','$attrvalue',$attrsize,$attrrow,$attrlenther,1";
			$this->db->query('INSERT INTO ' . $db_table . ' (' . $db_field . ') VALUES (' . $db_values . ')');
			$this->writelog($this->lng['modelmanage_attr_add_log'], $this->lng['log_extra_ok'] . ' typename=' . $typename);
			$this->dbcache->clearcache('modeatt_array_' . $mid, true);
			exit('true');
		} elseif ($inputclass == 'edit') {
			$this->powercheck(62, 88);
			$aid = $this->fun->accept('aid', 'P');
			$db_where = 'aid=' . $aid;
			$db_set = "typename='$typename',typeremark='$typeremark',inputtype='$inputtype',attrvalue='$attrvalue',attrsize=$attrsize,attrrow=$attrrow,attrlenther=$attrlenther";
			$this->db->query('UPDATE ' . $db_table . ' SET ' . $db_set . ' WHERE ' . $db_where);
			$this->writelog($this->lng['modelmanage_attr_edit_log'], $this->lng['log_extra_ok'] . ' typename=' . $typename . ' id=' . $id);
			$this->dbcache->clearcache('modeatt_array_' . $mid, true);
			exit('true');
		}
	}

	function ondelmodel() {
		$this->powercheck(62, 94);
		$db_table = db_prefix . 'model';
		$db_table2 = db_prefix . 'model_att';
		$id = $this->fun->accept('id', 'R');
		if (empty($id)) exit('false');
		$db_where = "mid=$id";
		$this->db->query('DELETE FROM ' . $db_table . ' WHERE ' . $db_where);
		$this->db->query('DELETE FROM ' . $db_table2 . ' WHERE ' . $db_where);
		$this->writelog($this->lng['modelmanage_del_log'], $this->lng['log_extra_ok'] . ' id=' . $selectinfoid);
		$this->dbcache->clearcache('model_array', true);
		$this->dbcache->clearcache('model_view_' . $id, true);
		$this->dbcache->clearcache('modeatt_array_' . $id, true);
		exit('true');
	}

	function ondelattr() {
		$this->powercheck(62, 89);
		$db_table = db_prefix . 'model_att';
		$selectinfoid = $this->fun->accept('attrselectinfoid', 'P');
		if (empty($selectinfoid)) exit('false');
		$infoarray = explode(',', $selectinfoid);
		$count = count($infoarray) - 1;
		if ($count <= 0) exit('false');
		for ($i = 0; $i < $count; $i++) {
			$db_where = "aid=$infoarray[$i]";
			$this->db->query('DELETE FROM ' . $db_table . ' WHERE ' . $db_where);
		}
		$this->writelog($this->lng['modelmanage_attr_log_del'], $this->lng['log_extra_ok'] . ' id=' . $selectinfoid);
		$this->dbcache->clearcache('modeatt_array', true);
		exit('true');
	}

	function onattrsort() {
		$this->powercheck(62, 91);
		$db_table = db_prefix . 'model_att';
		$id = $this->fun->accept('infoid', 'P');
		$pid = $this->fun->accept('pid', 'P');
		$idArray = explode(',', $id);
		$pidArray = explode(',', $pid);
		foreach ($idArray as $key => $value) {
			if (!empty($value)) {
				$db_where = "aid=$value";
				$pid = intval($pidArray[$key]);
				$db_set = "pid=$pid";
				$this->db->query('UPDATE ' . $db_table . ' SET ' . $db_set . ' WHERE ' . $db_where);
			}
		}
		$this->writelog($this->lng['modelmanage_attr_log_sort'], $this->lng['log_extra_ok']);
		$this->dbcache->clearcache('modeatt_array', true);
		exit('true');
	}

	function onmodelsetting() {
		$this->powercheck(62, 95);
		$db_table = db_prefix . 'model';
		$selectinfoid = $this->fun->accept('selectinfoid', 'P');
		$selectinfoid = $selectinfoid . '0';
		if (empty($selectinfoid)) exit('false');
		$value = $this->fun->accept('value', 'P');
		$dbname = $this->fun->accept('dbname', 'P');
		$db_set = "$dbname=$value";
		$db_where = "mid IN ( $selectinfoid )";
		$this->db->query('UPDATE ' . $db_table . ' SET ' . $db_set . ' WHERE ' . $db_where);
		$this->writelog($this->lng['modelmanage_log_istype'], $this->lng['log_extra_ok'] . ' id=' . $selectinfoid);
		$this->dbcache->clearcache('model_', true);
		exit('true');
	}

	function onattrsetting() {
		$this->powercheck(62, 90);
		$db_table = db_prefix . 'model_att';
		$selectinfoid = $this->fun->accept('attrselectinfoid', 'P');
		$selectinfoid = $selectinfoid . '0';
		if (empty($selectinfoid)) exit('false');
		$value = $this->fun->accept('value', 'P');
		$dbname = $this->fun->accept('dbname', 'P');
		$db_set = "$dbname=$value";
		$db_where = "aid IN ( $selectinfoid )";
		$this->db->query('UPDATE ' . $db_table . ' SET ' . $db_set . ' WHERE ' . $db_where);
		$this->writelog($this->lng['modelmanage_attr_log_istype'], $this->lng['log_extra_ok'] . ' id=' . $selectinfoid);
		$this->dbcache->clearcache('modeatt_array', true);
		exit('true');
	}

	function oncheckattrname() {
		$attlist = array('did', 'lng', 'pid', 'mid', 'aid', 'tid', 'sid', 'fgid', 'isclass', 'islink', 'ishtml', 'ismess', 'isorder',
		    'purview', 'istemplates', 'recommend', 'tsn', 'title', 'longtitle', 'color', 'author', 'source', 'pic', 'tags', 'keywords',
		    'description', 'link', 'oprice', 'bprice', 'click', 'addtime', 'uptime', 'template', 'filename', 'filepath', 'daid', 'picname'
		    , 'picfile', 'addtime', 'dcid', 'content', 'dlid', 'mid', 'labelname', 'doid', 'startime', 'endtime', 'width', 'height',
		    'istime', 'filetype', 'dvid');
		$attrname = $this->fun->accept('attrname', 'R');
		$mid = $this->fun->accept('mid', 'R');
		$attrnamearray = strtolower($attrname);

		if (in_array($attrnamearray, $attlist)) {
			exit('false');
		}
		if (!preg_match("/^[a-zA-Z]{2,50}$/i", $attrname)) {
			exit('false');
		}
		$db_table = db_prefix . 'model_att';
		$db_where = " WHERE attrname='$attrname' and mid=$mid";
		$countnum = $this->db_numrows($db_table, $db_where);
		if ($countnum > 0) {
			$exportAjax = 'false';
		} else {
			$exportAjax = 'true';
		}
		exit($exportAjax);
	}

}
?>